const app = getApp()
Page({
  data: {
    staticUrl: app.globalData.staticUrl,
    logoUrl: app.globalData.imagelogourl,
    avatarUrl: app.globalData.imageunlogurl,
    arraylist: [1, 2, 3, 4],
    markers: [],
    latitude: null,    // 地图中心纬度
    longitude: null,   // 地图中心经度
    distributorInfo: null, // 经销商详细信息
    controls: [{
      id: 1,
      iconPath: '/resources/location.png',
      position: {
        left: 0,
        top: 300 - 50,
        width: 50,
        height: 50
      },
      clickable: true
    }]
  },

  onLoad(options) {
    const ml_id = options.ml_id;

    console.log('ml_id--------------', ml_id);
    if (!ml_id) {
      wx.showToast({ 
        title: '参数错误', 
        icon: 'none',
        complete: () => {
          setTimeout(() => wx.navigateBack(), 1500);
        }
      });
      return;
    }
    this.getDistributorDetail(ml_id);
  },

  // 获取经销商详情
  getDistributorDetail(ml_id) {
    const that = this;
    wx.showLoading({ title: '加载中...' });
    
    wx.request({
      url: 'https://mapp.scoringchina.com/interface/xijiuopen/distributor/show',
      method: 'GET',
      data: { ml_id },
      success(res) {
        console.log('经销商详情接口返回数据:', res.data);
        console.log('经销商详情接口返回数据--errorcode--:', res.data.errorcode);
        if (res.data.errorcode.code == 'ok') {
          const data = res.data.data;
          // 方法一：基础分割方案
          const splitLocation = data.location.split(',');
          const longitude = parseFloat(splitLocation[0]); // 经度 112.915935
          const latitude = parseFloat(splitLocation[1]);  // 纬度 28.230070
          // 更新地图位置和标记
          that.setData({
            latitude: Number(latitude),
            longitude: Number(longitude),


            markers: [{
              id: 0,
              latitude: Number(latitude),
              longitude: Number(longitude),
              name: data.addr,
              callout: { content: data.name, display: 'ALWAYS' }
            }],
            distributorInfo: data
          });
        } else {
          wx.showToast({ title: res.data.msg || '数据加载失败', icon: 'none' });
        }
      },
      fail(err) {
        wx.showToast({ title: '网络请求失败', icon: 'none' });
      },
      complete() {
        wx.hideLoading();
      }
    });
  },

  // 拨打联系电话
  _opencall: function (e) {
    console.log('_opencall', e);
    const phone = e.currentTarget.dataset.tel;
    if (phone) {
      wx.makePhoneCall({
        phoneNumber: phone.toString()
      })
    } else {
      wx.showToast({ title: '暂无联系电话', icon: 'none' });
    }
  },

  // 其他原有生命周期函数保持不动
  onReady() {
    this.mapCtx = wx.createMapContext('myMap');
  },
  // ... 保留原有其他方法
});